# 等额本金、等额本息、先息后本、等本等息
# https://geetest1024.feishu.cn/docx/Z3xLdR0IqoMnZtxOjIocDwLPnSb
# 四种还款方式计算函数
from datetime import datetime
from dateutil.relativedelta import relativedelta
from pprint import pprint

def repayment_date_list(date_start=None, n=30 * 12):
    """
    默认分30年
    """
    if not date_start:
        date_now = datetime.now()
    else:
        date_now = date_start
    data_list = [date_now.date() + relativedelta(months=i) for i in range(n)]
    data_list = [item.strftime("%Y-%m-%d") for item in data_list]
    return data_list

def round_format(num):
    return round(num, 2)

# 等额本息
def deng_e_ben_xi_v1(principal, periods, monthly_rate):
    """
    等额本息计算规则
    如以商业性贷款20万元,贷款期为15年,则每月等额还本付息额为
    为5.58%÷12=4.65‰,还款期数为15X12=180
    即借款人每月向银行还款1642.66元,15年后,20万元的借款本息就全部还清。
    需要的参数
    
    principal = 20 * 10000
    number_periods = 10 * 12
    monthly_rate = 0.00465
    
    返回的数据类型
    """

    data_result = {
        "items": [],
        "principal": principal,
        "periods": periods,
        "monthly_rate": monthly_rate,
        "interest": 0,
    }
    t2 = monthly_rate * (1 + monthly_rate) ** periods
    t3 = (1 + monthly_rate) ** periods - 1
    t4 = principal * (t2 / t3)
    # 计算分期还款日期
    repayment_list = repayment_date_list(n=periods)
    items_list = [
        {
            "index": item_index + 1,
            "repayment_date": item,
            "amount": t4,
        }
        for item_index, item in enumerate(repayment_list)
    ]
    data_result["items"] = items_list
    data_result["sum_amount"] = t4 * periods
    data_result["sum_interest"] = t4 * periods - principal
    return data_result

# 等额本金
def deng_e_ben_jing_v1(principal, number_periods, monthly_rate):
    """
    principal = 20 * 10000
    number_periods = 10 * 12
    monthly_rate = 0.0049
    """
    data_result = {
        "items": [],
        "principal": principal,
        "number_periods": number_periods,
        "monthly_rate": monthly_rate,
        "interest": 0,
    }
    # 已经还的本金
    already_principal = 0
    # 已经还的利息
    already_interest = 0
    # 每期应还金额
    item_principal = principal / number_periods
    for item in range(1, number_periods + 1):
        already_principal = item_principal * (item - 1)
        item_interest = (principal - already_principal) * monthly_rate
        already_interest += item_interest
        item_amount = item_principal + item_interest
        # print(
        #     "第{}个月，应还本金{:.2f}，应还利息{:.2f}，当前应还总额{:.2f}，总共已还利息{:.2f}".format(
        #         item, item_principal, item_interest, item_amount, already_interest
        #     )
        # )
        data_result["items"].append({
            "index":item,
            "item_principal":item_principal,
            "item_interest":item_interest,
            "item_amount":item_amount,
            "already_interest":already_interest,
        })
    return data_result

def xian_ben_hou_xi(year_lilv, money, month):
    """
    等额本金
    year_lilv: 年化利率,0.45
    money: 贷款金额,10*10000
    month: 贷款月份,30
    
    返回的数据格式
    """
    year_lilv = float(year_lilv)
    money = float(money)
    month = int(month)
    object_list = []
    # 月还款额度
    interestM = 0
    # 累计还款总额
    interestTotal = 0
    # 每月偿还本金0 最后一个月偿还全部本金
    chbj = 0
    # 利息总额
    lxze = money * year_lilv / 12 * 0.01 * month
    print(lxze)
    for i in range(1, month + 1):
        # 第i月还款利息
        t1 = lxze / month
        # 最后一个月还款额 = 每月利息 + 贷款本金
        if i == month:
            chbj = money
            interestM = t1 + chbj
            print("interestM", interestM)
        else:
            print("t1", t1)

def deng_ben_deng_xi(year_lilv, money, month):
    """
    等额本息
    year_lilv: 年化利率,0.45
    money: 贷款金额,10*10000
    month: 贷款月份,30
    """
    year_lilv = float(year_lilv)
    money = float(money)
    month = float(month)

def deng_e_ben_jing(year_lilv, money, month):
    """
    先息后本
    year_lilv: 年化利率,0.45
    money: 贷款金额,10*10000
    month: 贷款月份,30
    """
    year_lilv = float(year_lilv)
    money = float(money)
    month = float(month)

def deng_e_ben_xi(year_lilv, money, month):
    """
    等本等息
    year_lilv: 年化利率,0.45
    money: 贷款金额,10*10000
    month: 贷款月份,30
    """
    year_lilv = float(year_lilv)
    money = float(money)
    month = float(month)


if __name__ == "__main__":
    year_lilv = 0.45
    money = 10*100
    month = 3
    
    xian_ben_hou_xi(year_lilv=year_lilv, money=money, month=month)
    # deng_ben_deng_xi(year_lilv=year_lilv, money=money, month=month)
    # deng_e_ben_jing(year_lilv=year_lilv, money=money, month=month)
    # deng_e_ben_xi(year_lilv=year_lilv, money=money, month=month)
